package com.kmt.plaza.controller;

import com.kmt.plaza.entity.Product;
import com.kmt.plaza.entity.UserAddress;
import com.kmt.plaza.sevice.UserAddressService;
import com.kmt.plaza.vo.ResStatus;
import com.kmt.plaza.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
@Api(value = "提供用户收货",tags = "地址管理")
@CrossOrigin
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;
    @ApiOperation(value = "按用户id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer",name = "userId",value = "用户id",required =false),
    })
    @GetMapping("/queryAddress")
    public ResultVO queryKey(@RequestParam("userId") Integer userId,@RequestHeader("token")String token){
        ResultVO resultVO = userAddressService.queryAddress(userId);
        return resultVO;
    }

    @ApiOperation(value = "添加用户地址接口")
    @PutMapping(value = "/insertAddress")
    public ResultVO insertProduct(@RequestBody UserAddress userAddress) {
        int i = userAddressService.insertAddress(userAddress);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",i);
        }else {
            return new ResultVO(ResStatus.OK,"success",null);
        }
    }
    @ApiOperation(value = "删出用户地址接口")
    @DeleteMapping(value = "/deleteAddress/{addressId}")
    public ResultVO deleteAddress(@PathVariable("addressId") Integer addressId) {
        return userAddressService.deleteAddress(addressId);
    }

    @ApiOperation(value = "修改地址信息接口")
    @PutMapping(value = "/updateAddress")
    public ResultVO updateUserInfo(@RequestBody UserAddress userAddress){
        int result = userAddressService.updateAddress(userAddress);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
    }
}
